/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.common.c;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import com.qihoo.gamecenter.sdk.common.i.i;
import com.qihoo.gamecenter.sdk.common.i.s;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import javax.net.ssl.SSLException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.params.HttpParams;
import org.json.JSONObject;

public class a {
    private String a;
    private Context b;
    private String[] c;

    private a(Context paramContext, String paramString) {
        this.a = paramString;
        this.b = paramContext;
        if (!(TextUtils.isEmpty(this.a)))
            return;
        this.a = s.u(this.b);
    }

    private String b() {
        String str1 = "";
        if ((this.c != null) && (this.c.length > 0))
            for (String str2 : this.c) {
                if ((TextUtils.isEmpty(str2)) || (str2.equalsIgnoreCase("null")))
                    continue;
                if (!(str2.endsWith(";")))
                    str2 = str2.concat(";");
                str1 = str1.concat(str2);
            }
        com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "the attached httpCookie = ", str1 });
        return str1;
    }

    public void a(String[] paramArrayOfString) {
        this.c = paramArrayOfString;
    }

    public String[] a() {
        return this.c;
    }

    public String a(String paramString1, String paramString2) {
        return a(paramString1, new b(paramString2) {
            public String a(HttpResponse paramHttpResponse, int paramInt) {
                String str = "fail";
                if (paramHttpResponse != null) {
                    InputStream localInputStream = null;
                    FileOutputStream localFileOutputStream = null;
                    int i = 0;
                    try {
                        if (paramInt == 200) {
                            File localFile1 = new File(this.a);
                            if (!(localFile1.exists()))
                                localFile1.createNewFile();
                            localFileOutputStream = new FileOutputStream(localFile1);
                            int j = 0;
                            byte[] arrayOfByte = new byte[2048];
                            localInputStream = b.a(paramHttpResponse.getEntity());
                            long l1 = 0L;
                            while ((j = localInputStream.read(arrayOfByte)) > 0) {
                                localFileOutputStream.write(arrayOfByte, 0, j);
                                l1 += j;
                            }
                            long l2 = paramHttpResponse.getEntity().getContentLength();
                            if (l2 == l1)
                                i = 1;
                        }
                    } catch (Exception localIOException4) {
                        com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor",
                                new Object[] { localException1.toString() });
                    } finally {
                        if (localInputStream != null)
                            try {
                                localInputStream.close();
                            } catch (IOException localIOException5) {
                                com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor",
                                        new Object[] { localIOException5.toString() });
                            }
                        if (localFileOutputStream != null)
                            try {
                                localFileOutputStream.close();
                            } catch (IOException localIOException6) {
                                com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor",
                                        new Object[] { localIOException6.toString() });
                            }
                    }
                    if (i != 0)
                        str = "success";
                    else
                        try {
                            File localFile2 = new File(this.a);
                            if (localFile2.exists())
                                localFile2.delete();
                        } catch (Exception localException2) {}
                }
                return str;
            }
        }, null);
    }

    public String a(String paramString, ArrayList paramArrayList) {
        return a(paramString, paramArrayList, null);
    }

    public String a(String paramString1, ArrayList paramArrayList, String paramString2) {
        UrlEncodedFormEntity localUrlEncodedFormEntity = null;
        try {
            localUrlEncodedFormEntity = new UrlEncodedFormEntity(paramArrayList, "UTF-8");
        } catch (Exception localException) {
            com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor", new Object[] { localException.toString() });
        }
        return a(paramString1, localUrlEncodedFormEntity, paramString2);
    }

    public String a(String paramString, HttpEntity paramHttpEntity) {
        return a(paramString, paramHttpEntity, "");
    }

    public String a(String paramString1, HttpEntity paramHttpEntity, String paramString2) {
        return a(paramString1, paramHttpEntity, new a(), false, paramString2);
    }

    public String a(String paramString1, HttpEntity paramHttpEntity, b paramb, boolean paramBoolean, String paramString2) {
        HttpPost localHttpPost = null;
        try {
            localHttpPost = new HttpPost(b.a(paramString1));
        } catch (Exception localException) {
            com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor", new Object[] { localException.toString() });
        }
        if ((localHttpPost != null) && (paramHttpEntity != null)) {
            localHttpPost.setEntity(paramHttpEntity);
            Header localHeader = paramHttpEntity.getContentType();
            com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "doPost --> content type = ",
                    (localHeader == null) ? "null" : localHeader.getValue() });
            if (localHeader != null)
                localHttpPost.setHeader(localHeader);
        }
        return a(localHttpPost, paramb, paramBoolean, paramString2);
    }

    public String a(String paramString1, b paramb, String paramString2) {
        return a(paramString1, paramb, false, paramString2);
    }

    public String a(String paramString) {
        return a(paramString, new a(), false, null);
    }

    public String b(String paramString1, String paramString2) {
        return a(paramString1, new a(), false, paramString2);
    }

    public String a(String paramString1, b paramb, boolean paramBoolean, String paramString2) {
        HttpGet localHttpGet = null;
        try {
            localHttpGet = new HttpGet(b.a(paramString1));
        } catch (Exception localException) {
            com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor", new Object[] { localException.toString() });
        }
        return a(localHttpGet, paramb, paramBoolean, paramString2);
    }

    public String a(HttpUriRequest paramHttpUriRequest, b paramb, boolean paramBoolean, String paramString) {
        Object localObject1 = null;
        f localf = null;
        int i = 1;
        String str1 = c.a(c.a.e);
        boolean bool1 = true;
        boolean bool2 = false;
        do {
            try {
                if ((paramHttpUriRequest == null) || (paramHttpUriRequest.getURI() == null))
                    throw new IllegalArgumentException("the 'request' is invalid");
                if ((bool1) && (paramHttpUriRequest.getURI().getHost().contains(".360.cn"))) {
                    localf = f.a(null, this.b);
                }
                else {
                    localf = f.b(null, this.b);
                    bool2 = true;
                }
                HttpParams localHttpParams = localf.getParams();
                HttpHost localHttpHost = b(b(this.b));
                if (localHttpHost != null)
                    localHttpParams.setParameter("http.route.default-proxy", localHttpHost);
                HttpClientParams.setRedirecting(localHttpParams, paramBoolean);
                if (!(paramHttpUriRequest.containsHeader("User-Agent")))
                    paramHttpUriRequest.setHeader("User-Agent", this.a);
                if (!(paramHttpUriRequest.containsHeader("Content-Type"))) {
                    com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor",
                            new Object[] { "Http Content-Type --> application/x-www-form-urlencoded" });
                    paramHttpUriRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
                }
                if (!(paramHttpUriRequest.containsHeader("Accept-Encoding"))) {
                    com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor",
                            new Object[] { "Http Accept-Encoding --> gzip, deflate" });
                    paramHttpUriRequest.setHeader("Accept-Encoding", "gzip, deflate");
                }
                String str2 = b();
                if ((!(paramHttpUriRequest.containsHeader("Cookie"))) && (!(TextUtils.isEmpty(str2)))) {
                    com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "Http Cookie --> ", str2 });
                    paramHttpUriRequest.setHeader("Cookie", str2);
                }
                int k = -1;
                HttpResponse localHttpResponse = localf.execute(paramHttpUriRequest);
                if (localHttpResponse != null) {
                    k = localHttpResponse.getStatusLine().getStatusCode();
                    this.c = b.a(localHttpResponse);
                    i = 0;
                    str1 = c.a(c.a.d);
                }
                com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "execute --> HttpStatusCode = ",
                        Integer.valueOf(k) });
                if (paramb != null) {
                    localObject1 = paramb.a(localHttpResponse, k);
                    com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "retContent=", localObject1 });
                    String str3 = paramHttpUriRequest.getURI().toURL().toString();
                    com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "url=", str3 });
                    if (TextUtils.isEmpty(paramString)) {
                        String str4 = com.qihoo.gamecenter.sdk.common.e.a.a(str3, (String) localObject1);
                        com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "decry=", str4 });
                        if (!(TextUtils.isEmpty(str4)))
                            localObject1 = str4;
                        com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "retContent=",
                                localObject1 });
                    }
                    else {
                        localObject1 = a(str3, paramString, (String) localObject1);
                    }
                }
            } catch (IllegalArgumentException localIllegalArgumentException) {
                i = 6;
                str1 = c.a(c.a.b);
            } catch (SSLException localSSLException) {
                com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor", new Object[] { localSSLException.toString() });
                i = 3;
                str1 = c.a(c.a.f);
                bool1 = false;
            } catch (ConnectTimeoutException localConnectTimeoutException) {
                com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor",
                        new Object[] { localConnectTimeoutException.toString() });
                i = 4;
                str1 = c.a(c.a.a);
            } catch (SocketTimeoutException localSocketTimeoutException) {
                com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor",
                        new Object[] { localSocketTimeoutException.toString() });
                i = 4;
                str1 = c.a(c.a.a);
            } catch (Exception localException1) {
                com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor", new Object[] { localException1.toString() });
                i = 2;
                str1 = c.a(c.a.c);
            } finally {
                if (localf != null)
                    localf.a();
            }
            com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor",
                    new Object[] { "useLocalCertificate=", Boolean.valueOf(bool1) });
            com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor",
                    new Object[] { "retryNoCertificate=", Boolean.valueOf(bool2) });
        } while ((!(bool1)) && (!(bool2)));
        int j = 1;
        if ((i == 0) && (!(TextUtils.isEmpty((CharSequence) localObject1))))
            try {
                new JSONObject((String) localObject1);
                j = 0;
            } catch (Exception localException2) {
                com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor", new Object[] { localException2.toString() });
            }
        if ((j != 0) && (s.h(this.b))) {
            i = 5;
            str1 = c.a(c.a.g);
        }
        com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "execute()  --> errorCode = ",
                i + ",  errorMsg = ", str1, ", content = ", localObject1 });
        return ((String) i.b(i, str1, localObject1));
    }

    public static a a(Context paramContext) {
        return a(paramContext, null);
    }

    public static a a(Context paramContext, String paramString) {
        return new a(paramContext, paramString);
    }

    private static String a(String paramString1, String paramString2, String paramString3) {
        com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "Key=", paramString2, "\ncontent=",
                paramString3 });
        if (!(TextUtils.isEmpty(paramString2)))
            return com.qihoo.gamecenter.sdk.common.i.f.d(paramString3, paramString2);
        return paramString3;
    }

    private static String b(Context paramContext) {
        String str = "";
        ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
                .getSystemService("connectivity");
        try {
            NetworkInfo localNetworkInfo = localConnectivityManager.getActiveNetworkInfo();
            if ((localNetworkInfo != null) && (localNetworkInfo.isAvailable()))
                str = localNetworkInfo.getExtraInfo();
        } catch (Exception localException) {
            com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor", new Object[] { localException.toString() });
        }
        return str;
    }

    public static HttpHost b(String paramString) {
        if (!(TextUtils.isEmpty(paramString))) {
            com.qihoo.gamecenter.sdk.common.i.c.a("HttpExecutor", new Object[] { "getApnProxy : ", paramString });
            paramString = paramString.toLowerCase();
            if ((paramString.startsWith("uniwap")) || (paramString.startsWith("3gwap")))
                return new HttpHost("10.0.0.172", 80);
            if (paramString.startsWith("cmwap"))
                return new HttpHost("10.0.0.172", 80);
            if (paramString.startsWith("ctwap"))
                return new HttpHost("10.0.0.200", 80);
        }
        return null;
    }

    public static abstract interface b {
        public abstract String a(HttpResponse paramHttpResponse, int paramInt);
    }

    public static final class a implements a.b {
        public String a(HttpResponse paramHttpResponse, int paramInt) {
            String str = null;
            if (paramHttpResponse != null) {
                InputStream localInputStream = null;
                try {
                    localInputStream = b.a(paramHttpResponse.getEntity());
                    str = b.b(localInputStream);
                } catch (Exception localIOException2) {
                    com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor", new Object[] { localException.toString() });
                } finally {
                    if (localInputStream != null)
                        try {
                            localInputStream.close();
                        } catch (IOException localIOException3) {
                            com.qihoo.gamecenter.sdk.common.i.c.b("HttpExecutor",
                                    new Object[] { localIOException3.toString() });
                        }
                }
            }
            return str;
        }
    }
}